<template>

  <div>
    <div v-if="screenWidth == 'no'">
      <h1>系统不支持低分辨率设备浏览，请更换设备再访问</h1>
    </div>
    <div v-if="screenWidth == 'pc'" style="height: 1000px">
      <el-row type="flex" class="row-bg" justify="center">
        <el-col :span="6"><div class="grid-content bg-purple">
          <h1>网上商城</h1>
          <img :src='userIamge' style="width: 200px;height: 200px;overflow: hidden;"alt="正在加载">
          <p>作者：王相</p>
          <h3>有问题请联系我，扫码</h3>
        </div></el-col>
        <el-col :span="1">
          <div class="grid-content bg-purple-light" >
            <el-divider direction="vertical" ></el-divider>
          </div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple">
          <el-form :model="loginForm" :rules="rules"  ref="loginForm" label-width="100px" class="demo-ruleForm" hide-required-asterisk>
            <el-form-item label="用户名" prop="username" style="width: 400px">
              <el-input v-model="loginForm.username" @change="getImage(loginForm.username)"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" style="width: 400px">
              <el-input v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="captcha" style="width: 400px;" >
              <el-input v-model="loginForm.captcha" style="width: 150px; float: left"></el-input>
              <el-image class="captchaImg" :src="captchaImg" @click="getCaptcha()"></el-image>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="login('loginForm')">登录</el-button>
              <el-button @click="resetForm('loginForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div></el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》'



import store from "@/store/index"


export default {
//import引入的组件需要注入到对象中才能使用

  components: {store},
  props: {},
  data() {
//这里存放数据
    return {
      hide: false,
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        //  随机码，由后端作为key返回,前端验证
        uuid: '123313'
      },
      captchaImg: null,
      data: [],
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},

        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 18, message: '密码长度为6到18位', trigger: 'blur'}
        ],
        captcha: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 5, max: 5, message: '验证码长度为5位', trigger: 'blur'}
        ]
      },
      userIamge:'https://shop-wangxiang.oss-cn-chengdu.aliyuncs.com/qr.png',
      screenWidth:null
    };
  },
//计算属性 类似于data概念
  computed: {

  },
//监控data中的数据变化
  watch: {},
//方法集合
  methods: {
//  登录

    login(loginForm){
      this.$refs[loginForm].validate((valid) => {
        if (valid){
          this.$http({
            url: 'sys/login',
            method: 'post',
            data: this.$http.adornData({
              loginId: this.loginForm.username,
              password: this.loginForm.password,
              uuid: this.loginForm.uuid,
              captcha: this.loginForm.captcha
            })
          }).then((res) => {
            //后端传入一个角色id
            sessionStorage.setItem("roleId",res.data.roleId)
            store.commit("setRoleId",res.data.roleId)
            sessionStorage.setItem("userId",res.data.userId)
              //因为有延迟所以采用缓存方式
            if(res.data.roleId!=0){
            }
            this.$router.push('/index')
          })
        }
      });
    },
    /**
     * 验证码
     * @param null
     */
    getCaptcha(){
      this.$http({
        url: 'sys/captcha',
        method: 'get'
      }).then((res) => {
      // console.log("验证码信息",res)
        this.captchaImg = res.data.captcha;
        this.loginForm.uuid = res.data.uuid;
      })
    },
    getMenu(){
      this.$http({
        url: 'sys/menu/getMenu',
        method: 'post',
        data: this.$http.adornData({
          id: sessionStorage.getItem("roleId")
        })
      }).then((res)=>{
        store.commit("setMenuList",res.data.menu)
      })
    },
    getImage(userName){
      // console.log("登录名",userName)
      // return this.imgUrl
      this.$http({
        url: 'sys/user/img',
        method: 'post',
        data:this.$http.adornData({
          loginId:userName
        })
      }).then((res)=>{
        // console.log("头像",res.data.img)
        if (res.data.img!=null){
          this.userIamge = res.data.img
        }
      })
    },
    resetForm(loginForm){
      // 使用组件loginForm,由ref定义,resetFields()，vue重置的封装方法
      this.$refs[loginForm].resetFields();
    },
    getScreen(){
      console.log(window.screen.width)
      if (window.screen.width > 1062){
        this.screenWidth = "pc"
        // return "pc"
        // return true
      }
      if (window.screen.width<1062){
        this.screenWidth = "no"

      }
    }


  },
//声明周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getScreen();
    this.getCaptcha();
  },
//声明周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>

.el-divider{
  height: 300px;
}
.el-row{
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(60, 63, 65, 0.1);
  width: 100%;
  height: 100%;
}
.captchaImg{
  float: left;
  border-radius: 5px;
  width: 130px;
  margin-left: 10px;
}
</style>
